import 'package:app/common/colors.dart';
import 'package:app/common/tool.dart';
import 'package:app/components/base_scaffold_appbar.dart';
import 'package:app/services/address.dart';
import 'package:app/services/dio_manager.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
class LeaveTypeView extends StatefulWidget {
  const LeaveTypeView({Key? key}) : super(key: key);

  @override
  State<LeaveTypeView> createState() => _LeaveTypeViewState();
}

class _LeaveTypeViewState extends State<LeaveTypeView> {


  LeaveTypeModel? model;

  requestData()async{
    var json = await DioManager().kkRequest(Address.hrLeaveType,method: 'get');

    LeaveTypeModel _model = LeaveTypeModel.fromJson(json);

    model = _model;

    setState(() {

    });
  }

  @override
  void initState() {
    super.initState();
    // assert(_debugLifecycleState == _StateLifecycle.created);
    requestData();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: BaseAppBar(
        preferredSize: Size(Get.width, AppTool.appBarHEIGHT),
        child: Text('请假类型'),
      ),
      body: ListView.builder(itemBuilder: (BuildContext context, int index) {
        return GestureDetector(
          onTap: (){
            Get.back(result: {'name':model?.data?[index].name,
              'id':model?.data?[index].id,'requestUnit':model?.data?[index].requestUnit});
          },
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Container(
                color: Colors.white,
                width: Get.width,
                padding: EdgeInsets.all(15),
                child: Text('${model?.data?[index].name}'),
              ),
              Container(
                height: 0.5,
                color: AppColor.lineColor,
              )
            ],
          ),
        );
      },itemCount: model?.data?.length??0,),
    );
  }
}


class LeaveTypeModel {
  int? errcode;
  String? errmsg;
  List<Data>? data;
  String? message;

  LeaveTypeModel({this.errcode, this.errmsg, this.data, this.message});

  LeaveTypeModel.fromJson(Map<String, dynamic> json) {
    errcode = json['errcode'];
    errmsg = json['errmsg'];
    if (json['data'] != null) {
      data = <Data>[];
      json['data'].forEach((v) {
        data!.add(new Data.fromJson(v));
      });
    }
    message = json['message'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['errcode'] = this.errcode;
    data['errmsg'] = this.errmsg;
    if (this.data != null) {
      data['data'] = this.data!.map((v) => v.toJson()).toList();
    }
    data['message'] = this.message;
    return data;
  }
}

class Data {
  int? id;
  String? name;
  String? requestUnit;

  Data({this.id, this.name, this.requestUnit});

  Data.fromJson(Map<String, dynamic> json) {
    id = json['id'];
    name = json['name'];
    requestUnit = json['request_unit'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['id'] = this.id;
    data['name'] = this.name;
    data['request_unit'] = this.requestUnit;
    return data;
  }
}
